/*
 *  Copyright 2011 yura.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.vcforms.editor.actions;

import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;
import org.opu.vcforms.actions.AbstractAction;

import org.opu.vcforms.editor.EditView;
import org.opu.vcforms.editor.TextView;
import org.opu.vcforms.resource.ResourceHandle;
import org.opu.vcforms.view.component.ButtonProperties;
import org.opu.vcforms.view.component.GUIProperties;

/**
 * 
 * @author yura
 */
public abstract class EditAction extends AbstractAction {

    private static final long serialVersionUID = 1783442947468599966L;

    public static final int YES = JOptionPane.YES_OPTION;
    public static final int NO = JOptionPane.NO_OPTION;
    public static final int CANCEL = JOptionPane.CANCEL_OPTION;

    /** last editor */
    protected EditView editView;

    public EditAction(EditView editView) {
        this.editView = editView;
    }

    public EditView getEditView() {
        return editView;
    }

    public void setEditView(EditView editView) {
        this.editView = editView;
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        if (editView != null) {
            action(editView.getSelectedText());
        }
    }

    @Override
    protected ButtonProperties getButtonProp() {
        return GUIProperties.instance().editor.button;
    }

    protected abstract void action(TextView tv);

    public int optionMsg(){
        Object[] options = {i18n.warningMsgYes(),
            i18n.warningMsgNo(),
            i18n.warningMsgCancel()};

        Object[] tltp = {i18n.warningMsgYesTltp(),
            i18n.warningMsgNoTltp(),
            i18n.warningMsgCancelTltp()};

        int ret = 0;

        try {
            ret = JOptionPane.showOptionDialog(null,
                    i18n.warningMsg(),
                    ResourceHandle.getProperty(ResourceHandle.APPLICATION_TITLE),
                    JOptionPane.YES_NO_CANCEL_OPTION,
                    JOptionPane.QUESTION_MESSAGE,
                    null,
                    options,
                    options[0]);
        } catch (Exception ex) {
            ret = JOptionPane.CANCEL_OPTION;
        }

        return ret;
    }
}
